package com.xiebishe.mylibrary.common.glide.glide;

import android.content.Context;
import android.graphics.drawable.PictureDrawable;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.Registry;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool;
import com.bumptech.glide.load.engine.cache.ExternalPreferredCacheDiskCacheFactory;
import com.bumptech.glide.load.engine.cache.LruResourceCache;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.module.AppGlideModule;
import com.bumptech.glide.request.RequestOptions;
import com.xiebishe.mylibrary.common.glide.ProgressManager;


import java.io.InputStream;

@GlideModule
public class MyGlideModule extends AppGlideModule {
    int diskSize = 104857600;

    public MyGlideModule() {
    }

    public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
        int maxMemory = (int)Runtime.getRuntime().maxMemory();
        int memoryCacheSize = maxMemory / 8;
        builder.setMemoryCache(new LruResourceCache(memoryCacheSize));
        builder.setBitmapPool(new LruBitmapPool(memoryCacheSize));
        builder.setDiskCache(new ExternalPreferredCacheDiskCacheFactory(context));
        builder.setDefaultRequestOptions((new RequestOptions()).format(DecodeFormat.PREFER_ARGB_8888));
    }

    public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
        registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(ProgressManager.getInstance().getOkHttpClient()));
        //registry.register(SVG.class, PictureDrawable.class, new SvgDrawableTranscoder()).append(InputStream.class, SVG.class, new SvgDecoder());
    }

    public boolean isManifestParsingEnabled() {
        return false;
    }
}

